﻿using BaseUi.Views.MainWindows;
using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi.Views.PlcView
{
    /// <summary>
    /// BlenderBoxView.xaml 的交互逻辑搅拌冷库页面
    /// </summary>
    public partial class BlenderBoxView : UserControlBase
    {
        private StudentData viewModel = new StudentData();
        private MachbujinData bujinModel = new MachbujinData();
        public SolidColorBrush BoroColor = new SolidColorBrush(Color.FromRgb(220, 220, 220));
        public SolidColorBrush GreenColor = new SolidColorBrush(Color.FromRgb(173, 255, 47));
        private bool bujin = false;
        public BlenderBoxView()
        {
            InitializeComponent();
            DataContext = bujinModel;
            bujinModel.CommonViewModel = MainWindow4.viewModel.CommonViewModel;
            viewModel.CommonViewModel = MainWindow4.viewModel.CommonViewModel;
        }

        private void loadFirst(object sender, EventArgs e)
        {
            if (this.IsInDesignMode)
                return;
            changclor(bujin);
            getyundong();
            getbujinjl();
            getdinwei();
            getjiaoban();
            Task Machbujin = new Task(() =>
            {
                while (true)
                {
                    getnewsan();
                    Thread.Sleep(200);
                }
            });
            Machbujin.Start();
        }
        /// <summary>
        /// 定位位置
        /// </summary>
        private void getdinwei()
        {
            var ioi = LocalStatic.Plc.Read<float>("1464");
            if (ioi.IsSucceed == false)
            {
                BenderBox2g1z1.Text = "未接收";
            }
            else
            {
                BenderBox2g1z1.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("1468");
            if (ioi.IsSucceed == false)
            {
                BenderBox2g1z2.Text = "未接收";
            }
            else
            {
                BenderBox2g1z2.Text = ioi.Value.ToString();
            }
            
        }
        /// <summary>
        /// 步进距离
        /// </summary>
        private void getbujinjl()
        {
            var io2 = LocalStatic.Plc.Read<float>("1466");
            if (io2.IsSucceed == false)
            {
                BlenderBox1g1z1.Text = "未接收";
            }
            else { BlenderBox1g1z1.Text = io2.Value.ToString(); }
            io2 = LocalStatic.Plc.Read<float>("1470");
            if (io2.IsSucceed == false)
            {
                BlenderBox1g1z2.Text = "未接收";
            }
            else { BlenderBox1g1z2.Text = io2.Value.ToString(); }
        }
        /// <summary>
        /// 搅拌
        /// </summary>
        private void getjiaoban()
        {
            var io2 = LocalStatic.Plc.Read<float>("1150");
            if (io2.IsSucceed == false)
            {
                BlenderBox1g2dz1.Text = "未接收";
            }
            else { BlenderBox1g2dz1.Text = io2.Value.ToString(); }
            var ioi2 = LocalStatic.Plc.Read<Int32>("1152");
            if (ioi2.IsSucceed == false)
            {
                BlenderBox1g2dz2.Text = "未接收";
            }
            else { BlenderBox1g2dz2.Text = ioi2.Value.ToString(); }
        }
        /// <summary>
        /// 运动允许误差
        /// </summary>
        private void getyundong()
        {
            var io2 = LocalStatic.Plc.Read<float>("1806");
            if (io2.IsSucceed==false)
            {
                BlenderBox2g2z1.Text = "未接收";
            }
            else { BlenderBox2g2z1.Text = io2.Value.ToString(); }
            io2 = LocalStatic.Plc.Read<float>("1808");
            if (io2.IsSucceed == false)
            {
                BlenderBox2g2z2.Text = "未接收";
            }
            else { BlenderBox2g2z2.Text = io2.Value.ToString(); }
        }
        /// <summary>
        /// 当前位置状态速度
        /// </summary>
        private void getnewsan()
        {
            var ioi = LocalStatic.Plc.Read<float>("18");
            if (ioi.IsSucceed==false)
            {
                bujinModel.weizhiu2 = "未接收";
            }
            else
            {
                bujinModel.weizhiu2 = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("20");
            if (ioi.IsSucceed == false)
            {
                bujinModel.weizhiu3 = "未接收";
            }
            else
            {
                bujinModel.weizhiu3 = ioi.Value.ToString();
            }

            var io2 = LocalStatic.Plc.Read<Int16>("67");
            var io3 = LocalStatic.Plc.Read<Int16>("68");
            bujinModel.ditionu2 = getnumber(io2.Value);
            bujinModel.ditionu3 = getnumber(io3.Value);

            ioi = LocalStatic.Plc.Read<float>("24");
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudu2 = "未接收";
            }
            else
            {
                bujinModel.sudu2 = ioi.Value.ToString();
            }
            ioi =  LocalStatic.Plc.Read<float>("54");
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudu3 = "未接收";
            }
            else
            {
                bujinModel.sudu3 = ioi.Value.ToString();
            }
        }
        private string getnumber(int ind)
        {
            string retul = "未接受信息";
            if (ind == 1)
            {
                retul = "未使能";
            }
            else if (ind == 2)
            {
                retul = "使能状态";
            }
            else if (ind == 3)
            {
                retul = "运动状态";
            }
            else if (ind == 4)
            {
                retul = "回原中";
            }
            else if (ind == 5)
            {
                retul = "轴故障";
            }
            return retul;
        }
        
        private void getveloc(int inder)
        {
            veloc1.Background = BoroColor;
            veloc2.Background = BoroColor;
            veloc3.Background = BoroColor;
            if (inder==0)
            {

                veloc1.Background = GreenColor;
            }
            else if (inder == 1)
            {

                veloc2.Background = GreenColor;
            }
            else if (inder == 2)
            {

                veloc3.Background = GreenColor;
            }
        }
        private void changclor(bool bucolor)
        {
            if (bujin)
            {
                butjog.Background = BoroColor;
                butbujin.Background = GreenColor;
            }
            else
            {
                butjog.Background = GreenColor;
                butbujin.Background = BoroColor;
            }
        }
        private void BenlderButton_Click1g1z1(object sender, RoutedEventArgs e)
        {
            bujin = false;
            changclor(bujin);
        }

        private void BenlderButton_Click1g1z2(object sender, RoutedEventArgs e)
        {
            bujin = true;
            changclor(bujin);
            getbujinjl();
        }

        private void BlenderButton_Click2g1z1(object sender, RoutedEventArgs e)
        {
                try
            {
                
                var ioi = LocalStatic.Plc.Write<bool>("79", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void BlenderButton_Click2g1z2(object sender, RoutedEventArgs e)
        {
            try
            {

                var ioi = LocalStatic.Plc.Write<bool>("80", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
        private void BlenderButton_Click2g2z1(object sender, RoutedEventArgs e)
        {
            try
            {

                var ioi = LocalStatic.Plc.Write<bool>("87", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void BlenderButton_Click2g2z2(object sender, RoutedEventArgs e)
        {
            try
            {

                var ioi = LocalStatic.Plc.Write<bool>("88", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 低速
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BlenderButton_Click3g2z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 0);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
                
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void BlenderButton_Click3g2z2(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 1);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
                
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

            private void BlenderButton_Click3g2z3(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 2);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
                
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// Y轴到安全位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BlenderButton_Click3g3z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("111", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        
        
        private void Checked(object sender, RoutedEventArgs e)
        {
             try
            {
                short ni = 1;
                if (blendertog.IsChecked == true)
                {
                    ni = 0;
                }
                else
                {
                    ni = 1;
                }
                var ioi = LocalStatic.Plc.Write<Int16>("1000", ni);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach1g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("77", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BlenderBox1g1z1.Text);
                    if (fait<0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1466", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("81", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach1g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("77", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach1g2d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("78", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BlenderBox1g1z1.Text);
                    if (fait > 0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1466", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("81", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach1g2p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("78", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach2g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("85", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BlenderBox1g1z2.Text);
                    if (fait<0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1470", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("89", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach2g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("85", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach2g2d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("86", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BlenderBox1g1z2.Text);
                    if (fait > 0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1470", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("89", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bormach2g2p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("86", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinu2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BlenderBox1g1z1.Text);
                var ioi = LocalStatic.Plc.Write<float>("1466", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void bujinu3(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BlenderBox1g1z2.Text);
                var ioi = LocalStatic.Plc.Write<float>("1470", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void TextBox_KeyDownu2(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    float fait = float.Parse(BenderBox2g1z1.Text);
                    if (LocalStatic.Plc.Write<float>("1464", fait).IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
                catch (Exception ee)
                {
                    Growl.Error(ee.ToString());
                }
            }
        }

        private void TextBox_KeyDownu3(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    float fait = float.Parse(BenderBox2g1z2.Text);
                    if (LocalStatic.Plc.Write<float>("1468", fait).IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
                catch (Exception ee)
                {
                    Growl.Error(ee.ToString());
                }
            }
        }

        private void yundongwucu2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BlenderBox2g2z1.Text);
                var ioi = LocalStatic.Plc.Write<float>("1806", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void yundongwucu3(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BlenderBox2g2z2.Text);
                var ioi = LocalStatic.Plc.Write<float>("1808", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void jioabanshud(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BlenderBox1g2dz1.Text);
                if (fait>1000) 
                {
                    Growl.Error("搅拌速度大于1000");
                    return;
                }
                if (fait<0)
                {
                    Growl.Error("搅拌速度小于0");
                    return;
                }
                var ioi = LocalStatic.Plc.Write<float>("1150", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void jioabansjian(object sender, MouseButtonEventArgs e)
        {
            try
            {
                int ser = Convert.ToInt32(BlenderBox1g2dz2.Text);
                if (ser < 0)
                {
                    Growl.Error("搅拌时间小于0");
                    return;
                }
                var ioi = LocalStatic.Plc.Write<Int32>("1152", ser);
                if (ioi.IsSucceed == true)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void shoudongButton_Click1g1z2(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("84", true);
                if (ioi.IsSucceed == true)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
    }
}
